package edu.core.testapp.client;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Protect01Widget extends Composite {
	
	private final Panel mainPanel = new VerticalPanel();
	
	private final TextBox keywordBox = new TextBox();
	private final TextArea dictViewBox = new TextArea();
	
	private final TextArea toBox = new TextArea();
	private final TextArea fromBox = new TextArea();
	
	private final Button toButton = new Button("to");
	private final Button fromButton = new Button("from");
	
	private final ListBox numberBox = new ListBox();
	
	private final Button setButton = new Button("set");
	
	private final Protect01ServiceAsync service = GWT.create(Protect01Service.class);
	
	public Protect01Widget() {
		
		for (int i = 0; i < 50; i++) {
			numberBox.addItem("" + i);
		}
		numberBox.setSelectedIndex(5);
		
		keywordBox.setText("DIPLOMAT");
		
		setButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				service.getDict(keywordBox.getText(), numberBox.getSelectedIndex(), new AsyncCallback<String>() {
					
					@Override
					public void onSuccess(String result) {
						// TODO Auto-generated method stub
						dictViewBox.setText(result);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						dictViewBox.setText(caught.getMessage());
					}
				});
			}
		});
		
		toBox.setText("Viva! Las Vegas!");
		
		HorizontalPanel keyPanel = new HorizontalPanel();
		keyPanel.add(numberBox);
		keyPanel.add(keywordBox);
		keyPanel.add(setButton);
		
		mainPanel.add(keyPanel);
		
		dictViewBox.setSize("500px", "100px");
		mainPanel.add(dictViewBox);
		
		HorizontalPanel textPanel = new HorizontalPanel();
		toBox.setSize("250px", "250px");
		fromBox.setSize("250px", "250px");
		textPanel.add(fromBox);
		textPanel.add(toBox);
		
		mainPanel.add(textPanel);
		
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.add(toButton);
		buttonPanel.add(fromButton);
		
		mainPanel.add(buttonPanel);
		
		toButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				service.encodeString(
						fromBox.getText(), 
						keywordBox.getText(), 
						new Integer(numberBox.getSelectedIndex()), 
						new AsyncCallback<String>() {
					
					@Override
					public void onSuccess(String result) {
						toBox.setText(result);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						toBox.setText(caught.getMessage());
					}
				});
			}
		});
		
		fromButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				service.decodeString(
						toBox.getText(), 
						keywordBox.getText(), 
						new Integer(numberBox.getSelectedIndex()), 
						new AsyncCallback<String>() {
							
							@Override
							public void onSuccess(String result) {
								fromBox.setText(result);
							}
							
							@Override
							public void onFailure(Throwable caught) {
								fromBox.setText(caught.getMessage());
							}
						}
				);
			}
		});
		
		initWidget(mainPanel);
	}

}
